package com.samphanie.security.validation.sms;

import cn.hutool.json.JSONObject;
import com.samphanie.security.domain.Captcha;
import com.samphanie.security.validation.properties.CodeConstant;
import com.samphanie.security.validation.service.AbstractValidateCodeProcessor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * 生成短信验证码
 *
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Component("smsCaptcha")
@RequiredArgsConstructor
public class SmsCodeProcessor extends AbstractValidateCodeProcessor<Captcha> {

    private final ISmsCodeSenderService smsCodeSenderService;

    /**
     * 发送校验码
     */
    @Override
    protected void send(ServletWebRequest request, Captcha captcha) {
        String paramMobile = CodeConstant.DEFAULT_PARAMETER_NAME_MOBILE;
        String paramKey = CodeConstant.DEFAULT_PARAMETER_NAME_KEY;

        String mobile = (String) request.getRequest().getAttribute(paramMobile);
        String key = (String) request.getRequest().getAttribute(paramKey);

        JSONObject obj = new JSONObject();
        obj.putOpt("code", captcha.getCode());
        smsCodeSenderService.send(key, mobile, String.valueOf(obj));
    }
}
